<?php
		global $db;
		include(BASE . "subsystems/getid3/getid3.php");
		$getid3 = new getID3();
		$max_item = 8;
		$page=$_GET['page'];
		
		// get module title
$module_container=$db->selectObject('container',"internal = '".serialize($loc)."'");
		
		$template = new template('composersmodule','Default',$loc);
		
		if (!defined('SYS_SORTING')) require_once(BASE.'subsystems/sorting.php');
		if (!defined('SYS_FILES')) require_once(BASE.'subsystems/files.php');
		
		$directory = 'files/composersmodule/' . $loc->src;
		if (!file_exists(BASE.$directory)) {
			$err = exponent_files_makeDirectory($directory);
			if ($err != SYS_FILES_SUCCESS) {
				$template->assign('noupload',1);
				$template->assign('uploadError',$err);
			}
		}
		
		$listings = $db->selectObjects('listing',"list_type=1 AND album_type = 1 AND location_data='".serialize($loc)."' LIMIT ".$page *  $max_item.",{$max_item}");
		for($i=0; $i<count($listings); $i++) {
			if ($listings[$i]->file_id == 0) {
				$listings[$i]->picpath = '';
			} else {
				$file = $db->selectObject('file', 'id='.$listings[$i]->file_id);
				$listings[$i]->picpath = $file->directory.'/'.$file->filename;
			}
			
			// các album
			//
			$musics=$db->selectObjectsBySql("SELECT website_musics.* FROM website_listing  INNER JOIN website_musics  ON website_musics.composer_id = website_listing.id WHERE website_listing.id={$listings[$i]->id} LIMIT 0,5");
			for($j=0; $j<count($musics); $j++) {
				$album=$db->selectObject('listing','id ='.$musics[$j]->album_id);
	$musics[$j]->downloadable=$album->downloadable;
				if ($musics[$j]->high_quality == 0) {
				$musics[$j]->high_quality = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->high_quality);
				$musics[$j]->high_quality = $file->directory.'/'.$file->filename;
				if (file_exists($musics[$j]->high_quality))
				{
				 $file_info = @$getid3->analyze($musics[$j]->high_quality); 
				  $file_info=$file_info['playtime_string'];
				  $musics[$j]->file_length = $file_info;
				 }
				}
				
				if ($musics[$j]->sound_trackmix == 0) {
				$musics[$j]->sound_trackmix = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->sound_trackmix);
				$musics[$j]->sound_trackmix = $file->directory.'/'.$file->filename;
				}
				
				if ($musics[$j]->sheet_music == 0) {
				$musics[$j]->sheet_music = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->sheet_music);
				$musics[$j]->sheet_music = $file->directory.'/'.$file->filename;
				}
				
				if ($musics[$j]->demo == 0) {
				$musics[$j]->demo = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->demo);
				$musics[$j]->demo = $file->directory.'/'.$file->filename;
				}
				
				// get composer name
				$composer=$db->selectObject('listing',"list_type = 1 AND id = {$musics[$j]->composer_id}");
				$composer_name='';
				if ($composer)
				{
					$composer_name=$composer->name;
				}
				
				// for tooltip
				$musics[$j]=musicsmodule::make_info($musics[$j], $composer_name, $album->name);
				
			}
			$listings[$i]->musics=$musics;
		}
		
		// get all listings here
		$count_post = $db->countObjects('listing',"list_type=1 AND location_data='".serialize($loc)."'");
		
		// page split
		// cal how many posts
			$page_count=floor($count_post / $max_item);
			if ($count_post % $max_item != 0)
			  $page_count++;
			// build page array
			$pages = array();
			for ($i=1;$i<=$page_count;$i++)
				$pages[$i-1] = $i;
		
		//sort the listings by their rank
		usort($listings, 'exponent_sorting_byNameAscending');
		
		$template->register_permissions(array('administrate','configure'),$loc);
		$template->assign('listings', $listings);
		$template->assign('moduletitle', $module_container->title);
		$template->assign('page_count',$page_count);
		$template->assign('pages',$pages);
		$template->assign('page',$page);
		$template->assign('cur_page',$page);
		$template->output();

?>
